/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import util.SpeakkerPlaylistUtil;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Album.findAll", query = "SELECT a FROM Album a ORDER BY a.title  ASC"),
    @NamedQuery(name = "Album.getById", query = "SELECT a FROM Album a WHERE a.id=:id"),
    @NamedQuery(name = "Album.getByTitleAndArtist", query = "SELECT a FROM Album a WHERE a.title=:title AND a.artist=:artist")})
public class Album implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String coverImage;
    private String title;
    @ManyToOne
    private Artist artist;
    @ManyToMany
    private Collection<Genre> genres;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date releaseDate; // the year of the album is the year of the release date
    private int totalTime; // in secondes
    private int nbTimesPlayed;
    @OneToMany
    private Collection<Music> musics;
    @OneToMany
    private Collection<Comment> comments;

    //
    // Constructors
    //
    public Album() {
    }

    public Album(String coverImage, String title, Artist artist, Date releaseDate) {
        this.coverImage = coverImage;
        this.title = title;
        this.artist = artist;
        this.genres = new ArrayList<Genre>();
        this.releaseDate = releaseDate;
        
        this.totalTime = 0;
        this.nbTimesPlayed = 0;
        
        this.musics = new ArrayList<Music>();
        this.comments = new ArrayList<Comment>();
    }

    //
    // Accessors
    //
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Artist getArtist() {
        return artist;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public Collection<Genre> getGenres() {
        return genres;
    }

    public void setGenres(Collection<Genre> genres) {
        this.genres = genres;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date date) {
        this.releaseDate = date;
    }

    public String getCoverImage() {
        return coverImage;
    }

    public void setCoverImage(String coverImage) {
        this.coverImage = coverImage;
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }

    public int getNbTimesPlayed() {
        return nbTimesPlayed;
    }

    public void setNbTimesPlayed(int nbPlayed) {
        this.nbTimesPlayed = nbPlayed;
    }

    public Collection<Music> getMusics() {
        return musics;
    }

    public void setMusics(Collection<Music> musics) {
        this.musics = musics;
    }

    public Collection<Comment> getComments() {
        return comments;
    }

    public void setComments(Collection<Comment> comments) {
        this.comments = comments;
    }
    
    public void addMusic(Music music) {
        musics.add(music);
        totalTime += music.getTotalTime();
    }
    
    public void addGenre(Genre genre) {
        genres.add(genre);
    }
    
    public String getTotalTimeStr() {
        String minutes = String.valueOf(totalTime/60);
        String secondes = String.valueOf(totalTime%60);
        
        return minutes + ":" + secondes;
    }
    
    // Get the playlist under JSon format supported by the javascript Speakker player
    public String getJSon() {
        return SpeakkerPlaylistUtil.getJson(musics);
    }

    public void incPlayCount() {
        nbTimesPlayed++;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Album)) {
            return false;
        }
        Album other = (Album) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Album[ id=" + id + " ]";
    }
}
